// https://v1.jinrishici.com/all.json

import { getPoetry } from '@/apis'

/** 获取诗词 */
export async function usePoetry() {
  const { data, send } = useRequest(getPoetry, {
    initialData: {
      author: '',
      category: '',
      content: '',
      origin: ''
    },
    immediate: false
  })

  await send()

  const poetryContent = computed(() => data.value.content)
  const poetryAuthor = computed(() => data.value.author)
  const poetryCategory = computed(() => data.value.category)
  const poetryOrigin = computed(() => data.value.origin)

  const intervalSend = (/** 请求时间 */ time = 1000) => {
    return useIntervalFn(() => {
      send()
    }, time)
  }

  const timeoutSend = (/** 请求时间 */ time = 1000) => {
    return useTimeoutFn(() => {
      send()
    }, time)
  }

  return {
    /** 诗词 */
    content: poetryContent,

    /** 作者 */
    author: poetryAuthor,

    /** 诗词类别 */
    category: poetryCategory,

    /** 诗词来源 */
    origin: poetryOrigin,
    poetryData: data,

    /** 发送请求 */
    send,

    /** 间断性发起请求 */
    intervalSend,

    /** N秒后发起请求 */
    timeoutSend
  }
}
